﻿package com.athena.process
{
	import com.athena.controller.AuctionController;
	import com.greensock.motionPaths.MotionPath;
	import flash.display.*;
	import flash.utils.*;
	import flash.text.TextFormat;
	import flash.events.*;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import com.adobe.serialization.json.JSON;
	import flash.net.URLRequest ;
	import flash.net.URLVariables ;
	import flash.net.URLRequestMethod ;
	import flash.net.URLRequestHeader ;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat ;
	import flash.net.sendToURL;
	import flash.net.navigateToURL;
	/**
	 * ...
	 * @author ...
	 */
	public class LotteryFunction 
	{
		public static var TabSelect : int = 1 ;		
		
		public static var scrX35 : ScrollPane = new ScrollPane(); 
		public static var scrollX35 : MovieClip;
		public static var scrX75 : ScrollPane = new ScrollPane(); 
		public static var scrollX75 : MovieClip;	
		
		public static var scrWin : ScrollPane = new ScrollPane(); 
		public static var scrollWin : MovieClip;
		public static var arrX35 : Array = null ;
		public static var arrX75 : Array = null ;
		
		//public static var AGAuction : int = 0 ;
		public static var urlHistory : String = "http://services.athena.vn/ServiceGame/XosoHistory.aspx?uid=" ;
		public static var urlHistoryWin : String = "http://services.athena.vn/ServiceGame/XosoHistoryWin.aspx" ;
		public static var urlResult : String = "http://services.athena.vn/ServiceGame/Xoso.aspx" ;
		
		public static var isLoad : Boolean = false ;
				
		public static function Lottery_Click(evt : MouseEvent) {
			//Cocaro.tf.appendText("=>Lottery:") ;
			var popup : MovieClip = MovieClip(new Cocaro.Popup_Xoso) ;
			//Cocaro.tf.appendText("=>1") ;
			popup.Datcuoc_btn.addEventListener(MouseEvent.CLICK, Datcuoc_Click) ;
			popup.Gioithieu_btn.addEventListener(MouseEvent.CLICK, Gioithieu_Click) ;
			popup.Close_btn.addEventListener(MouseEvent.CLICK, Close_Click) ;
			popup.Datcuoc_btn.gotoAndStop(2) ;
			popup.Gioithieu_btn.gotoAndStop(1) ;
			//Cocaro.tf.appendText("=>2") ;
			popup.Datcuoc_mc.visible = true ;
			popup.Gioithieu_mc.visible = false ;
			popup.Datcuoc_mc.AG_txt.text = "" ;
			popup.Datcuoc_mc.Num_txt.text = "" ;
			popup.Datcuoc_mc.rbType1.selected = false ;
			popup.Datcuoc_mc.rbType2.selected = false ;
			//Cocaro.tf.appendText("=>3") ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 12;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			//Cocaro.tf.appendText("=>4") ;
			popup.Datcuoc_mc.AG_txt.setStyle("textFormat", tfStyle);
			popup.Datcuoc_mc.Num_txt.setStyle("textFormat", tfStyle);
			popup.Datcuoc_mc.Send_btn.addEventListener(MouseEvent.CLICK, Send_Click) ;
			popup.Datcuoc_mc.X35_btn.addEventListener(MouseEvent.CLICK, X35_Click) ;
			popup.Datcuoc_mc.X75_btn.addEventListener(MouseEvent.CLICK, X75_Click) ;
			//Cocaro.tf.appendText("=>5") ;
			isLoad = false ;
			popup.name = "Lottery" ;
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;
			//Cocaro.tf.appendText("=>LotteryFinish") ;
			LoadHistory() ;
			LoadResult() ;
		}
		public static function X35_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;	
			popup.Datcuoc_mc.rbType1.selected = true ;
			popup.Datcuoc_mc.rbType2.selected = false ;
		}
		public static function X75_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;			
			popup.Datcuoc_mc.rbType1.selected = false ;
			popup.Datcuoc_mc.rbType2.selected = true ;
		}
		public static function Close_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ";	
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";	
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		
		public static function Datcuoc_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			popup.Datcuoc_btn.gotoAndStop(2) ;
			popup.Gioithieu_btn.gotoAndStop(1) ;
			popup.Datcuoc_mc.visible = true ;			
			popup.Gioithieu_mc.visible = false ;
		}
		public static function Gioithieu_Click(evt : MouseEvent) {			
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			popup.Datcuoc_btn.gotoAndStop(1) ;
			popup.Gioithieu_btn.gotoAndStop(2) ;
			popup.Datcuoc_mc.visible = false ;
			popup.Gioithieu_mc.visible = true ;
			if (!isLoad)
				LoadHistoryWin() ;
		}				
		
		public static function LotteryOK(obj : Object) {
			//Cocaro.tf.appendText("=>OK:") ;
			////Cocaro.tf.appendText("=>Count:" + obj.T) ;
			BasicFunction.RemoveHideLoading() ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			//if (obj.LQ > 0)
			//	Cocaro.currPlayer.MarkLQ = obj.AG ;
			var rbType : Object = popup.Datcuoc_mc.rbType1.group ;
			//Cocaro.currPlayer.MarkLQ = obj.LQ ;
			Cocaro.currPlayer.Mark = obj.AG ;
			var str : String = "Bạn đã đặt thành công loại thẻ " ;
			if (rbType.selection.value == 35)
				str = str + "X3.5 " ;
			else 
				str = str + "X75 " ;
			str = str + " cho số " + BasicFunction.numberFormat(parseInt(popup.Datcuoc_mc.Num_txt.text), 0, false, true) + " với giá trị là " + BasicFunction.numberFormat(parseInt(popup.Datcuoc_mc.AG_txt.text) * 1000, 0, false, true) + " AG." ;
			BasicFunction.ShowAlert(str) ;
			popup.Datcuoc_mc.Num_txt.text = "" ;
			popup.Datcuoc_mc.AG_txt.text = "" ;
			LoadHistory() ;
		}
		
		public static function CheckValid(str : String) : Boolean {
			for (var i:int = 0; i < str.length; i++) 
			{
				if ((str.charAt(i) != "0") && (str.charAt(i) != "1") && (str.charAt(i) != "2") && (str.charAt(i) != "3") && (str.charAt(i) != "4") 
					&& (str.charAt(i) != "5") && (str.charAt(i) != "6") && (str.charAt(i) != "7") && (str.charAt(i) != "8") && (str.charAt(i) != "9")) return false ;
			}
			return true ;
		}
		
		public static function Send_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			//Cocaro.tf.appendText("=>Start Price") ;
			if (Cocaro.currPlayer.VIP < 2) {
				BasicFunction.ShowAlert("Bạn phải từ Vip 2 trở lên mới được tham gia trò chơi XSLQ!") ;
				return ;
			}
			if (popup.Datcuoc_mc.Num_txt.text.length != 2) {
				BasicFunction.ShowAlert("Số bạn nhập phải là số có 2 chữ số!") ;
				return ;
			}
			if (!CheckValid(popup.Datcuoc_mc.Num_txt.text)) {
				BasicFunction.ShowAlert("Số bạn chọn không hợp lệ!") ;
				return ;
			}
			if (!CheckValid(popup.Datcuoc_mc.AG_txt.text)) {
				BasicFunction.ShowAlert("AG bạn đặt không hợp lệ!") ;
				return ;
			}
			if (isNaN(parseInt(popup.Datcuoc_mc.AG_txt.text))) {
				BasicFunction.ShowAlert("AG bạn nhập không hợp lệ!") ;
				return ;
			}
			if (isNaN(parseInt(popup.Datcuoc_mc.Num_txt.text))) {
				BasicFunction.ShowAlert("Số bạn nhập không hợp lệ!") ;
				return ;
			}
			if ((popup.Datcuoc_mc.AG_txt.text == "") || (popup.Datcuoc_mc.Num_txt.text == "")) {
				BasicFunction.ShowAlert("Bạn phải nhập đầy đủ AG đặt và số bạn chọn!") ;
				return ;
			}
			if (parseInt(popup.Datcuoc_mc.AG_txt.text) <= 0) {
				BasicFunction.ShowAlert("AG bạn nhập phải lớn hơn 0") ;
				return ;
			}			
			var rbType : Object = popup.Datcuoc_mc.rbType1.group ;
			if (rbType.selection == null) {
				BasicFunction.ShowAlert("Bạn phải chọn loại thẻ đặt cược!") ;
				return ;
			}
			if ((popup.Datcuoc_mc.rbType1.selected == false) && (popup.Datcuoc_mc.rbType2.selected == false)) {
				BasicFunction.ShowAlert("Bạn phải chọn loại thẻ đặt cược!") ;
				return ;
			}
			var obj : Object = new Object() ;
			obj.evt = "xs" ;
			obj.AG = parseInt(popup.Datcuoc_mc.AG_txt.text) * 1000 ;
			obj.N = popup.Datcuoc_mc.Num_txt.text ;
			obj.T = rbType.selection.value ;
			if ((obj.T == 35) && (obj.AG > 25000000)) {
				BasicFunction.ShowAlert("Bạn chỉ được đặt tối đa 25,000,000 AG cho loại thẻ X3.5!") ;
				return ;
			}
			if ((obj.T == 75) && (obj.AG > 5000000)) {
				BasicFunction.ShowAlert("Bạn chỉ được đặt tối đa 5,000,000 AG cho loại thẻ X75!") ;
				return ;
			}
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			//Cocaro.tf.appendText("=>SenđG:" + JSON.encode(obj)) ;
			//var mc : MovieClip = MovieClip(new Loading()) ;
			//mc.x = 0 ;
			//mc.y = 0 ;
			//mc.name = "Loading" ;
			//Cocaro.mainStage_mc.addChild(mc) ;
		}
		/***************************** Result *************************************************/
		public static function ResultReceive(str : String) {
			//Cocaro.tf.appendText("=>ResultReceive:" + str) ;
			var obj : Object = JSON.decode(str) ;
			var strResult : String = obj.KQ ;
			//Cocaro.tf.appendText("=>KQ:" + strResult) ;
			var arr : Array = strResult.split(";")
			//Cocaro.tf.appendText("=>Length:" + arr.length) ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			if (arr == null) return ;
			if (arr.length != 28) return ;
			var dtDate : Date = new Date(obj.D) ;
			popup.Datcuoc_mc.Title_txt.text = "Kết quả ngày " + dtDate.getDate() + "/" + (dtDate.getMonth() + 1).toString() + "/" + dtDate.getFullYear() ;
			popup.Datcuoc_mc.Giai0_1_txt.text = arr[0] ;
			popup.Datcuoc_mc.Giai1_1_txt.text = arr[1] ;
			
			popup.Datcuoc_mc.Giai2_1_txt.text = arr[2] ;
			popup.Datcuoc_mc.Giai2_2_txt.text = arr[3] ;
			
			popup.Datcuoc_mc.Giai3_1_txt.text = arr[4] ;
			popup.Datcuoc_mc.Giai3_2_txt.text = arr[5] ;
			popup.Datcuoc_mc.Giai3_3_txt.text = arr[6] ;
			popup.Datcuoc_mc.Giai3_4_txt.text = arr[7] ;
			popup.Datcuoc_mc.Giai3_5_txt.text = arr[8] ;
			popup.Datcuoc_mc.Giai3_6_txt.text = arr[9] ;
			
			popup.Datcuoc_mc.Giai4_1_txt.text = arr[10] ;
			popup.Datcuoc_mc.Giai4_2_txt.text = arr[11] ;
			popup.Datcuoc_mc.Giai4_3_txt.text = arr[12] ;
			popup.Datcuoc_mc.Giai4_4_txt.text = arr[13] ;
			
			popup.Datcuoc_mc.Giai5_1_txt.text = arr[14] ;
			popup.Datcuoc_mc.Giai5_2_txt.text = arr[15] ;
			popup.Datcuoc_mc.Giai5_3_txt.text = arr[16] ;
			popup.Datcuoc_mc.Giai5_4_txt.text = arr[17] ;
			popup.Datcuoc_mc.Giai5_5_txt.text = arr[18] ;
			popup.Datcuoc_mc.Giai5_6_txt.text = arr[19] ;
			
			popup.Datcuoc_mc.Giai6_1_txt.text = arr[20] ;
			popup.Datcuoc_mc.Giai6_2_txt.text = arr[21] ;
			popup.Datcuoc_mc.Giai6_3_txt.text = arr[22] ;
			
			popup.Datcuoc_mc.Giai7_1_txt.text = arr[23] ;
			popup.Datcuoc_mc.Giai7_2_txt.text = arr[24] ;
			popup.Datcuoc_mc.Giai7_3_txt.text = arr[25] ;
			popup.Datcuoc_mc.Giai7_4_txt.text = arr[26] ;
			////Cocaro.tf.appendText("=>Ketquafinished") ;
		}
		public static function LoadResult() {
			//Cocaro.tf.appendText("=>KetquaSend:" + urlResult) ;
			var request:URLRequest = new URLRequest(urlResult);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			//Cocaro.tf.appendText("==>Loading..") ;
			function onComplete(event:Event):void {
				ResultReceive(event.target.data) ;
			}
		}	
		/***************************** Ket qua *************************************************/
		public static function HistoryReceive(str : String) {
			//Cocaro.tf.appendText("=>HistoryReceive:" + str) ;
			var arr : Array = (JSON.decode(str) as Array) ;
			////Cocaro.tf.appendText("=>Length:" + arr.length) ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			scrollX35 = new MovieClip() ;
			scrX35.source = scrollX35; 
			scrX35.setSize(150, 180); 			
			scrX35.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrX35.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrX35.move(27, 262);
			popup.Datcuoc_mc.addChild(scrX35) ;
			scrollX75 = new MovieClip() ;
			scrX75.source = scrollX75; 
			scrX75.setSize(150, 180); 			
			scrX75.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrX75.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrX75.move(196, 262);			
			popup.Datcuoc_mc.addChild(scrX75) ;
			////Cocaro.tf.appendText("=>Length:" + arr.length) ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			var dem35 : int = 0 ;
			var dem75 : int = 0 ;
			//Add item moi
			for (var i:int = 0; i < arr.length; i++) 
			{
				var mc : MovieClip = MovieClip(new Cocaro.Lottery_Item) ;
				mc.x = 0 ;				
				mc.Num_txt.text = arr[i].N ;
				mc.AG_txt.text = BasicFunction.numberFormat(arr[i].AG,0,false, true) ;
				if (mc.Num_txt.text.length == 1) mc.Num_txt.text = "0" + mc.Num_txt.text ;
				mc.name = "LotteryItem" ;
				if (arr[i].T == 3.5) {
					mc.y = dem35 * 18 ;
					scrollX35.addChild(mc) ;
					scrX35.update() ;
					dem35++ ;					
				} else {
					mc.y = dem75 * 18 ;
					scrollX75.addChild(mc) ;
					scrX75.update() ;
					dem75++ ;					
				}
			}
			//Cocaro.tf.appendText("=>Length:" + scrX35.) ;
		}
		public static function LoadHistory() {
			////Cocaro.tf.appendText("=>KetquaSend:" + urlKetqua) ;
			var request:URLRequest = new URLRequest(urlHistory + Cocaro.currPlayer.PlayerID);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete(event:Event):void {
				HistoryReceive(event.target.data) ;
			}
		}		
		/***************************** Lich su ************************************************/
		public static function HistoryWinReceive(str : String) {
			//Cocaro.tf.appendText("=>Lichsu:" + str) ;
			isLoad = true ;
			var arr : Array = (JSON.decode(str) as Array) ;
			////Cocaro.tf.appendText("==>P:" + obj.P) ;
			////Cocaro.tf.appendText("==>AS:" + obj.AS) ;
			//var arr : Array = (JSON.decode(obj.AS) as Array) ;
			////Cocaro.tf.appendText("==>1") ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lottery")) ;
			if (popup == null) return ;
			scrollWin = new MovieClip() ;
			scrWin.source = scrollWin; 
			scrWin.setSize(435, 365); 			
			scrWin.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrWin.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrWin.move(514, 22);
			popup.Gioithieu_mc.addChild(scrWin) ;			
			////Cocaro.tf.appendText("=>Length:" + obj.AS.length) ;
			//Add item moi
			for (var i:int = 0; i < arr.length; i++) 
			{
				var mc : MovieClip = MovieClip(new Cocaro.Lottery_ItemWin) ;
				mc.Name_txt.text = arr[i].N ;
				mc.AG_txt.text = BasicFunction.numberFormat(arr[i].AG, 0, false, true) ;
				mc.Time_txt.text = arr[i].T ;
				mc.TT_txt.text = (i + 1).toString() ;
				mc.name = "LotteryItemWin" ;
				mc.x = 0 ;
				mc.y = i * 20 ;
				scrWin.addChild(mc) ;
				scrWin.update() ;
			}
		}
		public static function LoadHistoryWin() {
			//if (objAuction == null) return ;
			//if (objAuction.Id == null) return ;
			var request:URLRequest = new URLRequest(urlHistoryWin);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete(event:Event):void {
				HistoryWinReceive(event.target.data) ;
			}
		}		
	}
}